package br.com.paiva.container.controller;

import java.util.List;

import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;

import br.com.paiva.container.Aeroporto;
import br.com.paiva.container.service.AeroportoService;
import br.com.paiva.container.util.AeroportoUtilits;

@Controller
public class AeroportoController {	
	
	@Autowired
	@Qualifier("aeroportoService")
	private AeroportoService aeroportoService;
	
	private Aeroporto aeroporto = new Aeroporto();
	
	public void setAeroportoService(AeroportoService aeroportoService) {
		this.aeroportoService = aeroportoService;
	}
	
	public String save(){
		
		this.aeroportoService.save(this.aeroporto);
		this.aeroporto=null;
		this.aeroporto = new Aeroporto();
		return "success";
		
	}
	
	public List<Aeroporto> getAeroportos(){
		return this.aeroportoService.list();
	}
	
	public void retrive(ActionEvent event){
		Long id = AeroportoUtilits.getIdLink(event);
		this.aeroporto = this.aeroportoService.getAeroportoById(id);
	}
		
	public String delete(ActionEvent event){
		Long id = AeroportoUtilits.getIdLink(event);
		this.aeroporto = this.aeroportoService.getAeroportoById(id);
		return "sucess";
	}
		
	public String back(){
		this.aeroporto = null;
		this.aeroporto = new Aeroporto();
		return "back";
	}

	public Aeroporto getAeroporto() {
		return aeroporto;
	}

	public void setAeroporto(Aeroporto aeroporto) {
		this.aeroporto = aeroporto;
	}
	
	public String search(){
		
		this.aeroportoService.save(this.aeroporto);
		this.aeroporto=null;
		this.aeroporto = new Aeroporto();
		return "success";
		
	}
	
	public List<String> searchAeroportos(Object event){
		return aeroportoService.searchAeroporto(event.toString());
	}
	
}
